package com.cangshukj.web.controller.system;

import com.cangshukj.common.annotation.Log;
import com.cangshukj.common.core.controller.BaseController;
import com.cangshukj.common.core.domain.AjaxResult;
import com.cangshukj.common.enums.BusinessType;
import com.cangshukj.system.domain.SysRegion;
import com.cangshukj.system.domain.vo.RegionTreeSelect;
import com.cangshukj.system.service.ISysRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区Controller
 * 
 * @author cangshukj
 * @date 2022-03-08
 */
@Api(value = "/system/region",description = "地区")
@RestController
@RequestMapping("/system/region")
public class SysRegionController extends BaseController
{
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询地区列表
     */
    @ApiOperation("查询地区列表")
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public AjaxResult list(SysRegion sysRegion)
    {
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        List<SysRegion> ls = new ArrayList<>();
        for(SysRegion region:list){
            int level = region.getLevel();
            boolean hasChildren = true;
            if(level == 4){
                hasChildren = false;
            }
            region.setHasChildren(hasChildren);
            ls.add(region);
        }
        return AjaxResult.success(ls);
    }

    /**
     * 获取地区详细信息
     */
    @ApiOperation("获取地区详细信息")
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{regionid}")
    public AjaxResult getInfo(@PathVariable("regionid") String regionid)
    {
        return AjaxResult.success(sysRegionService.selectSysRegionByRegionid(regionid));
    }
    /**
     * 根据pid获取父级下的地区信息
     */
    @ApiOperation("根据pid获取子级地区信息")
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/pid")
    public AjaxResult getInfo1(String parentId)
    {
        return AjaxResult.success(sysRegionService.selectSysRegionByPid(parentId));
    }

    /**
     * 新增地区
     */
    @ApiOperation("新增地区")
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion)
    {
        int rows = sysRegionService.insertSysRegion(sysRegion);
        return toAjax(rows);
    }

    /**
     * 修改地区
     */
    @ApiOperation("修改地区")
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion)
    {
        return toAjax(sysRegionService.updateSysRegion(sysRegion));
    }

    /**
     * 激活地区
     */
    @ApiOperation("激活地区")
    @PreAuthorize("@ss.hasPermi('system:region:activa')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping("/activa")
    public AjaxResult editActiva(@RequestBody SysRegion sysRegion)
    {
        return toAjax(sysRegionService.updateActivaStatus(sysRegion));
    }


    /**
     * 删除地区
     */
    @ApiOperation("删除地区")
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{regionids}")
    public AjaxResult remove(@PathVariable String[] regionids)
    {
        return toAjax(sysRegionService.deleteSysRegionByRegionids(regionids));
    }

    /**
     * 获取地区下拉树列表
     */
    @ApiOperation("获取地区下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysRegion sysRegion)
    {
        List<SysRegion> regions = sysRegionService.selectRegionLevelList(sysRegion);
        List<RegionTreeSelect> treeSelects = sysRegionService.buildRegionTreeSelect(regions);
        return AjaxResult.success(treeSelects);
    }

    /**
     * 根据regionid获取地区下拉树列表
     */
    @ApiOperation("根据获取地区下拉树列表")
    @GetMapping("/treeselectById")
    public AjaxResult treeselectById(String regionid)
    {
        List<SysRegion> regions = sysRegionService.selectSysRegionByPid(regionid);
        List<RegionTreeSelect> treeSelects = sysRegionService.buildRegionTreeSelect(regions);
        return AjaxResult.success(treeSelects);
    }

}

